<?php
// 文件上传
$newFilename='';
// var_dump($_FILES["pic"]["error"]);
if ($_FILES["file"]["error"]!==0) {
    echo json_encode(["code"=>1,"msg"=>"上传失败，请重新上传"],JSON_UNESCAPED_UNICODE);
} else {
    $name=$_FILES["file"]["name"];
    $filename=pathinfo($name);
    $extension=$filename["extension"];
    if (!in_array($extension, ["jpg","png","jpeg","gif"])) {
        echo json_encode(["code"=>1,"msg"=>"文件格式不正确"], JSON_UNESCAPED_UNICODE);
        die();
    }
    $newFilename=time().rand(1, 99).".".$extension;
    $newAddress="./upload/".$newFilename;
    move_uploaded_file($_FILES["file"]["tmp_name"], $newAddress);
    $data=["code"=>0,"pic"=>$newFilename];
    echo json_encode($data, JSON_UNESCAPED_UNICODE);
}

